

set more off
mat drop _all


use "$path_data/data/temp/main_reg_data_psc.dta", clear



gen dummy_age_missing = 1 if age==.
recode dummy_age_missing(.=0)
gen age_nonmis = age if dummy_age_missing == 0
replace age_nonmis = 0 if dummy_age_missing == 1
gen age_sq_nonmis = age_sq if dummy_age_missing == 0
replace age_sq_nonmis = 0 if dummy_age_missing == 1

save "$path_data/data/temp/main_reg_data_psc_hetero_age.dta", replace








* 8 years old
qui teffects ipw (dif_psc) (treatment student_gender age_nonmis age_sq_nonmis, probit) if ptsbl_overall_score != . & age == 8 & grade ==2, atet
parmest, label list(parm label estimate min* max* p) saving(results_1, replace)
use results_1, clear
replace parm = "1" in 2
keep in 2
gen grade = 2
save "$path_data/data/temp/results_1.dta", replace
clear




use "$path_data/data/temp/main_reg_data_psc_hetero_age.dta", clear
* 9 years old
qui teffects ipw (dif_psc) (treatment student_gender age_nonmis age_sq_nonmis, probit) if ptsbl_overall_score != . & age == 9 & grade ==2, atet
parmest, label list(parm label estimate min* max* p) saving(results_2, replace)
use results_2, clear
replace parm = "2" in 2
keep in 2
gen grade = 2
save "$path_data/data/temp/results_2.dta", replace
clear


use "$path_data/data/temp/main_reg_data_psc_hetero_age.dta", clear
* 10 years old
qui teffects ipw (dif_psc) (treatment student_gender age_nonmis age_sq_nonmis, probit) if ptsbl_overall_score != . & age == 10 & grade == 2, atet
parmest, label list(parm label estimate min* max* p) saving(results_3, replace)
use results_3, clear
replace parm = "3" in 2
keep in 2
gen grade = 2
save "$path_data/data/temp/results_3.dta", replace
clear



use "$path_data/data/temp/main_reg_data_psc_hetero_age.dta", clear
* 11 years old
qui teffects ipw (dif_psc) (treatment student_gender age_nonmis age_sq_nonmis, probit) if ptsbl_overall_score != . & age == 11 & grade == 2, atet
parmest, label list(parm label estimate min* max* p) saving(results_4, replace)
use results_4, clear
replace parm = "4" in 2
keep in 2
gen grade = 2
save "$path_data/data/temp/results_4.dta", replace
clear





use "$path_data/data/temp/results_1.dta"
forvalues i = 2/4 {
append using "$path_data/data/temp/results_`i'.dta"
}
forvalues i = 1/4 {
capture rm "$path_data/data/temp/results_`i'.dta"
}
 
save "$path_data/data/temp/results_grade3.dta", replace








use "$path_data/data/temp/main_reg_data_psc_hetero_age.dta", clear
* 9 years old
qui teffects ipw (dif_psc) (treatment student_gender age_nonmis age_sq_nonmis, probit) if ptsbl_overall_score != . & age == 9 & grade ==4, atet
parmest, label list(parm label estimate min* max* p) saving(results_2, replace)
use results_2, clear
replace parm = "2" in 2
keep in 2
gen grade = 4
save "$path_data/data/temp/results_2.dta", replace
clear


use "$path_data/data/temp/main_reg_data_psc_hetero_age.dta", clear
* 10 years old
qui teffects ipw (dif_psc) (treatment student_gender age_nonmis age_sq_nonmis, probit) if ptsbl_overall_score != . & age == 10 & grade == 4, atet
parmest, label list(parm label estimate min* max* p) saving(results_3, replace)
use results_3, clear
replace parm = "3" in 2
keep in 2
gen grade = 4
save "$path_data/data/temp/results_3.dta", replace
clear



use "$path_data/data/temp/main_reg_data_psc_hetero_age.dta", clear
* 11 years old
qui teffects ipw (dif_psc) (treatment student_gender age_nonmis age_sq_nonmis, probit) if ptsbl_overall_score != . & age == 11 & grade == 4, atet
parmest, label list(parm label estimate min* max* p) saving(results_4, replace)
use results_4, clear
replace parm = "4" in 2
keep in 2
gen grade = 4
save "$path_data/data/temp/results_4.dta", replace
clear






use "$path_data/data/temp/main_reg_data_psc_hetero_age.dta", clear
* 12 years old
qui teffects ipw (dif_psc) (treatment student_gender age_nonmis age_sq_nonmis, probit) if ptsbl_overall_score != . & age == 12 & grade == 4, atet
parmest, label list(parm label estimate min* max* p) saving(results_5, replace)
use results_5, clear
replace parm = "5" in 2
keep in 2
gen grade = 4
save "$path_data/data/temp/results_5.dta", replace
clear


use "$path_data/data/temp/results_2.dta"
forvalues i = 3/5 {
append using "$path_data/data/temp/results_`i'.dta"
}
forvalues i = 2/5 {
capture rm "$path_data/data/temp/results_`i'.dta"
}
 
save "$path_data/data/temp/results_grade4.dta", replace

append using "$path_data/data/temp/results_grade3.dta"




destring parm, replace
label define age_hetero 1 "8 yo" 2 "9 yo" 3 "10 yo" 4 "11 yo" 5 "12 yo"  
label values parm age_hetero

label variable estimate "regression coefficient"
label variable min95 "lower 95% CI "
label variable max95 " upper 95% CI"
twoway (scatter estimate parm if grade == 2, mcolor(black) msymbol(square) msize(small)) (rcap min95 max95 parm if grade == 2, lcolor(black)) ///
(line estimate parm if grade == 2, lcolor(black)) ///
(scatter estimate parm if grade == 4, mcolor(black) msymbol(triangle) msize(small)) (rcap min95 max95 parm if grade == 4, lcolor(black) lpattern(solid) msize(huge)) ///
(line estimate parm if grade == 4, lcolor(black) lpattern(dash)) ///
, xlabel(, valuelabels angle(0) labsize(small)) ylabel(, angle(3)) xtitle("Quantile of Baseline PTS-II Score") ytitle("Treatment Effects") legend(order(1 "Point Estimation for Grade 3" 2 "Upper/Lower 95% CI for Grade 3" 4 "Point Estimation for Grade 4" 5 "Upper/Lower 95% CI for Grade 4") margin(midium) size(*.9)) graphregion(color(white))


graph export "$pardir/figureH2.pdf", replace as(pdf)




clear












